<?php
/**
 * file video.php
 * Created on 2015-3-25 下午4:51:29 
 * Create by xiweijie
 */
namespace haibao\www\data;

use \haibao\www\model\filter\Video as VideoFilter;
use \haibao\www\model\filter\VideoProperty as VideoPropertyFilter;
use \haibao\frame\data\query\Condition;
use \haibao\www\model\filter\VideoRelTag as VideoRelTagFilter;
class Video extends BaseMysql{
    public function __construct(){
    
        parent::__construct('\haibao\www\model\data\Video');
    }
    public function addVideo($model){
        $this->beginTranscation();
        try{
            $this->add($model);
            $this->addVideoProperty($model);
            $this->addVideoTelTag($model);
        
        } catch(\Exception $e){
        
            $this->rollback();
            throw $e;
        
        }
        $this->endTranscation();
        return $model;
    }

    public function addVideoProperty($model)
    {
        if (! empty($model->VideoPropertyModel)) {
            $model->VideoPropertyModel->VideoId = $model->Id;
            $this->add($model->VideoPropertyModel);
        }
    }

    public function getVideoById($id)
    {
        $videoFilter = new VideoFilter();
        $videoPropertyFilter = new VideoPropertyFilter();
        $videoFileFilter = new \haibao\cms\model\filter\VideoFile();
        
        $videoFilter->select(array(
            VideoFilter::CONDITION_FIELD_NAME_ID,
            VideoFilter::CONDITION_FIELD_NAME_TITLE,
            VideoFilter::CONDITION_FIELD_NAME_VIDEOURL,
            VideoFilter::CONDITION_FIELD_NAME_URLH5,
            VideoFilter::CONDITION_FIELD_NAME_LASTMODIFYUSER,
            VideoFilter::CONDITION_FIELD_NAME_LASTMODIFYTIME,
            VideoFilter::CONDITION_FIELD_NAME_PUBLISHDATE,
            VideoFilter::CONDITION_FIELD_NAME_CATEGORYID,
            VideoFilter::CONDITION_FIELD_NAME_SOURCE,
            VideoFilter::CONDITION_FIELD_NAME_DETAILURL,
            VideoFilter::CONDITION_FIELD_NAME_DESCRIPTION,
            VideoFilter::CONDITION_FIELD_NAME_PICID,
            VideoFilter::CONDITION_FIELD_NAME_PUBLISHED
        ));
        $videoPropertyFilter->select(array(
            VideoPropertyFilter::CONDITION_FIELD_NAME_COMMENTNUM,
            VideoPropertyFilter::CONDITION_FIELD_NAME_ID,
            VideoPropertyFilter::CONDITION_FIELD_NAME_PLAYNUM,
        	VideoPropertyFilter::CONDITION_FIELD_NAME_UPNUM,
            VideoPropertyFilter::CONDITION_FIELD_NAME_PLAYNUMSHOW,
            VideoPropertyFilter::CONDITION_FIELD_NAME_VIDEOID
        ));
        $videoFileFilter->select(array(
            \haibao\cms\model\filter\VideoFile::CONDITION_FIELD_NAME_ID,
            \haibao\cms\model\filter\VideoFile::CONDITION_FIELD_NAME_VIDEOID,
            \haibao\cms\model\filter\VideoFile::CONDITION_FIELD_NAME_STATUS,
            \haibao\cms\model\filter\VideoFile::CONDITION_FIELD_NAME_FILEPATH,
        	\haibao\cms\model\filter\VideoFile::CONDITION_FIELD_NAME_SECTION
        ));
        
        $videoFilter->leftJoin($videoPropertyFilter, null, VideoFilter::CONDITION_FIELD_NAME_ID,VideoPropertyFilter::CONDITION_FIELD_NAME_VIDEOID,VideoFilter::CONDITION_FIELD_NAME_PROPERTY,false);
        $videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_EQUAL,$id);
        $videoFilter->leftJoin($videoFileFilter,null,VideoFilter::CONDITION_FIELD_NAME_ID,\haibao\cms\model\filter\VideoFile::CONDITION_FIELD_NAME_VIDEOID,VideoFilter::CONDITION_FIELD_NAME_VIDEOFILE,false);
        $video = parent::getOne($videoFilter);
        return $video;
    }
    
    public function getVideoTagById($id){
    	$videoRelTagFilter = new VideoRelTagFilter();    	
    	$videoRelTagFilter->select(array(
    			VideoRelTagFilter::CONDITION_FIELD_NAME_TAGID,
    			VideoRelTagFilter::CONDITION_FIELD_NAME_VIDEOID,
    			VideoRelTagFilter::CONDITION_FIELD_NAME_ISPARIMARY
    	));
    	if(is_array($id)){
    		$videoRelTagFilter->where(VideoRelTagFilter::CONDITION_FIELD_NAME_VIDEOID, Condition::CONDITION_IN, $id);
    	}else{
    		$videoRelTagFilter->where(VideoRelTagFilter::CONDITION_FIELD_NAME_VIDEOID, Condition::CONDITION_EQUAL, $id);
    	}
    	$videoTag = $this->getAll($videoRelTagFilter);
    	return $videoTag;
    }
    
    public function updateVideoById($model){
        $this->beginTranscation();
        try{
            parent::updateById($model->Id, array(
                VideoFilter::CONDITION_FIELD_NAME_CATEGORYID=>$model->CategoryId,
                VideoFilter::CONDITION_FIELD_NAME_DETAILURL=>$model->DetailUrl,
                VideoFilter::CONDITION_FIELD_NAME_PICID=>$model->PicId,
                VideoFilter::CONDITION_FIELD_NAME_PUBLISHDATE=>$model->PublishDate,
                VideoFilter::CONDITION_FIELD_NAME_SOURCE=>$model->Source,
                VideoFilter::CONDITION_FIELD_NAME_TITLE=>$model->Title,
                VideoFilter::CONDITION_FIELD_NAME_URLH5=>$model->VideoUrlForH5,
                VideoFilter::CONDITION_FIELD_NAME_VIDEOURL=>$model->VideoUrl,
                VideoFilter::CONDITION_FIELD_NAME_DESCRIPTION=>$model->Description,
                VideoFilter::CONDITION_FIELD_NAME_LASTMODIFYTIME=>$model->LastModifyTime,
                VideoFilter::CONDITION_FIELD_NAME_LASTMODIFYUSER=>$model->LastModifyUser,
                VideoFilter::CONDITION_FIELD_NAME_PUBLISHED=>$model->Published
            ));
            
            $tagSql ="delete from VideoRelTag where VideoId=%s";
            $this->query($tagSql,array($model->Id));
            $this->addVideoTelTag($model);
            	
        }catch (\Exception $e) {
            $this->rollback();
            throw $e;
        }
        $this->endTranscation();
    }
    
    public function addVideoTelTag($model){
        if(!empty($model->VideorelTagModel)){
          $arrTagModel = $model->VideorelTagModel;
          foreach ($arrTagModel as $tagModel){
              $tagModel->VideoId = $model->Id;  
          }
          $this->addAll($arrTagModel);
        }
    }
    
    public function addVideoUpNum($vid){
        $sql = "update VideoProperty set UpNum=UpNum+1 where VideoId=%s";
        $result = $this->query($sql,array($vid));
    }
    
    public function addVideoPlayNum($vid){
        $sql = "update VideoProperty set PlayNum=PlayNum+1,PlayNumShow=PlayNumShow+1 where VideoId=%s";
        $result = $this->query($sql,array($vid));
    }
    
    public function getVideoUpNum($vid){
    	$videoPropertyFilter = new VideoPropertyFilter();
    	$videoPropertyFilter->select(array(
    			VideoPropertyFilter::CONDITION_FIELD_NAME_UPNUM,
    			VideoPropertyFilter::CONDITION_FIELD_NAME_ID
    	));
    	$videoPropertyFilter->where(VideoPropertyFilter::CONDITION_FIELD_NAME_VIDEOID, Condition::CONDITION_EQUAL, $vid);
    	$videoUpNum = parent::getOne($videoPropertyFilter);
    	return $videoUpNum; 
    }
}
